﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using System.Linq;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;
using ECRMS.WinForm.BaseClass.Other;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace ECRMS.WinForm.Modules
{
    [ModuleInstance((int)ModuleEnum.指标分配)]
    public partial class TargetAssignModule : BaseModule
    {
        public bool IsModified = false;//默认没有被修改过

        public Guid ModifyIndexGroupId;//当前在编辑的指标组id

        public TargetAssignModule()
        {
            base.core.CreateWaitDialog("初始化中请稍候...", "");
            InitializeComponent();
            InitView();
            InitData();
            InitEventHandler();
            base.core.CloseWaitDialog();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        private void InitView()
        {
            base.newBarButtonVisiable = false;
            base.updateBarButtonVisiable = false;
            base.delBarButtonVisiable = false;
            base.selectBarButtonVisiable = false;
            base.otherBarButton1Visiable = false;
            base.otherBarButton2Visiable = false;
            base.otherBarButton3Visiable = false;
            base.outBarButtonVisiable = false;

        }

        private void InitData(bool isPostBack = false)
        {
            InitEmployeeCombox();
            if (!isPostBack)
            {
                FuncResult result = core.CoreIndexPlan.GetIndexGroups(core.CoreUser.UserInfo.CorporationRefId,core.CoreUser.UserInfo.UserTypeId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }
            this.gdindexgroup.DataSource = core.CoreIndexPlan.IndexGroups;
            this.gdindexgroup.RefreshDataSource();
            this.ModifyIndexGroupId = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId");
            BindIndexs();
        }

        private void InitEmployeeCombox()
        {
            FuncResult result = core.CoreEmployee.GetEmployees(core.CoreUser.UserInfo.CorporationRefId);
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }
            ComboBoxItemCollection coll = employeeCombox.Properties.Items;
            coll.BeginUpdate();
            coll.Clear();

            coll.Add(new ComboxData { Text = "", GuidValue = Guid.Empty });

            foreach (CtEmployee item in core.CoreEmployee.Employees)
            {
                coll.Add(new ComboxData
                {
                    Text = item.UserInfo.Name,
                    GuidValue = item.UserInfo.UserRefId
                });
            }
            coll.EndUpdate();
        }

        private void BindIndexs()
        {
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId");

            FuncResult result = core.CoreIndexPlan.GetIndexs(rowIndex);
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }

            treeIndexs.DataSource = core.CoreIndexPlan.OperatorRowIndexs;
            treeIndexs.ExpandAll();
            //treeIndexs.CollapseAll();
        }

        private void repositoryItemImageComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            //得到id
            int id = (int)(this.treeIndexs.FocusedNode.GetValue("ID"));
            //得到是否是锁定状态
            bool isLock = (bool)(this.treeIndexs.FocusedNode.GetValue("Mark"));
            Single oldPeopleId = (Single)(this.treeIndexs.FocusedNode.GetValue("Length(cm)"));
            if (isLock)
            {
                ImageComboBoxEdit edit = sender as ImageComboBoxEdit;
                if ((Single)(edit.Value) == oldPeopleId)
                    return;
                edit.Value = oldPeopleId;
                XtraMessageBox.Show("此指标已经评分，所以不能分配给其他人，如果需要分配给其他人....再讨论一下。");

            }

        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:

                    break;
                case LangEnum.English:
                case LangEnum.英文:

                    break;
                default:
                    break;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Guid currentRowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId");
            if (currentRowIndex != ModifyIndexGroupId && IsModified)//切换指标组的之后如果存在原先指标组的数据已经修改过
            {
                DialogResult result = XtraMessageBox.Show(this, "指标组已有修改，是否保存修改的数据？", "保存数据", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)//确定保存
                {
                    FuncResult result1 = core.CoreIndexPlan.UpdateIndexs();
                    if (result1.Status == 0)
                    {
                        XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                        return;
                    }
                    IsModified = false;
                    ModifyIndexGroupId = currentRowIndex;
                    BindIndexs();
                }
                else if (result == DialogResult.No)
                {
                    IsModified = false;
                    ModifyIndexGroupId = currentRowIndex;
                    BindIndexs(); 
                }
            }
            else
            {
                IsModified = false;
                ModifyIndexGroupId = currentRowIndex;
                BindIndexs(); 
            };
        }

        private void employeeCombox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxEdit comboBox = sender as ComboBoxEdit;
            Guid selectRowIndexRefId = core.CoreCommon.GetTreeKeyValue(treeIndexs, "CorpIndexRefId");

          if (comboBox.SelectedIndex != -1)
            {
                Guid employeeRefId = (comboBox.SelectedItem as ComboxData).GuidValue;
                string employeeName = (comboBox.SelectedItem as ComboxData).Text;
                //CtIndex aaa = core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId);
                core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).AssignedUserRefId = employeeRefId;
                core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).AssignedUserName = employeeName;
                ResetBollowNodeState(core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).ID, employeeRefId, employeeName);
                treeIndexs.RefreshDataSource();
               // treeIndexs.Refresh();
            }
            else
            {
                core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).AssignedUserRefId = Guid.Empty;
                core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).AssignedUserName = "";
                ResetBollowNodeState(core.CoreIndexPlan.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId).ID, Guid.Empty, "");
                treeIndexs.RefreshDataSource();
            }
            IsModified = true;

        }
        //遍历指标id下面的子指标集合
        private void ResetBollowNodeState(Guid indexRefId, Guid employeeRefId, string employeeName)
        {
            List<CtIndex> childNodes = core.CoreIndexPlan.OperatorRowIndexs.Where(p => p.ParentID == indexRefId).ToList();
            if (childNodes != null && childNodes.Count() > 0)
            {
                foreach (CtIndex item in childNodes)
                {
                    item.AssignedUserRefId = employeeRefId;
                    item.AssignedUserName = employeeName;
                    ResetBollowNodeState(item.ID, employeeRefId, employeeName);
                }
            }
        }

        public override void saveBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButton_ItemClick(sender, e);
            DialogResult result = XtraMessageBox.Show(this, "确定要保存修改的数据？", "保存数据", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)//确定保存
            {
                FuncResult result1 = core.CoreIndexPlan.UpdateIndexs();
                if (result1.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                    return;
                }
                IsModified = false;
                treeIndexs.DataSource = core.CoreIndexPlan.OperatorRowIndexs;
                treeIndexs.RefreshDataSource();
                treeIndexs.ExpandAll();
            }
            else
            {
                return;
            }

        }
    }
}
